<template>
  <div class="container">
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 表单 -->
      <el-form :inline="true" ref="form" size="small" :span="5" :model="tagForm" label-width="80px">
        <el-form-item label="目录名称">
          <el-input v-model="queryData.directoryName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryData.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearState" size="small" style="margin-left: 6px;">清除</el-button>
          <el-button @click="getTagsList" type="primary" size="small">搜索</el-button>
        </el-form-item>
        <el-button @click="showDialog" size="small" type="success" style="float: right;">
          新增标签
          <i class="el-icon-edit"></i>
        </el-button>
      </el-form>
      <!-- 警告提示框 -->
      <el-alert :title="'数据一共'+ total +'条'" type="info" show-icon :closable="false"></el-alert>
      <!-- table表单 -->
      <el-table :data="tagsList">
        <el-table-column type="index" label="序号" ></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column  label="标签名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" prop="addDate">
          <template slot-scope="scope">{{ scope.row.addDate }}</template>
        </el-table-column>
        <el-table-column label="状态" prop="state">
          <template slot-scope="scope">{{ scope.row.state === 0 ? '已启用' : '已禁用'}}</template>
        </el-table-column>
       <el-table-column width="180" label="操作">
          <template slot-scope="scope">
            <el-button @click="showActive(scope.row)"  type="text" v-if="scope.row.state === 0" class="active">启用</el-button>
          <el-button  @click="showActive(scope.row)" type="text" v-else class="active">禁用</el-button>
          <el-button @click="editDirectory(scope.row)" type="text" :disabled="scope.row.state===1?true:false">修改</el-button>
          <el-button @click="deleteDirectory(scope.row)" type="text" :disabled="scope.row.state===1?true:false">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      class="el-pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryData.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 新增和修改不要弄混了 -->
    <!-- 新增标签跳出对话框 -->
    <el-dialog title="新增目录" :visible.sync="addTagsDialog" width="30%">
      <el-form :rules="addTagsFormRules" ref="addTagsForm" :model="addTagsForm">
        <el-form-item label="所属学科" prop="subjectID" label-width="80px">
          <el-select v-model="addDirectoryInfo.subjectID" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in subjectList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName" label-width="80px">
          <el-input placeholder="请输入目录名称" v-model="addDirectoryInfo.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTagsDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitadd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改对话框 -->
      <el-dialog
        title="修改标签"
        :visible.sync="editDialogVisible"
        width="400px">
        <el-form :rules="editRules" :model="editDirectoryInfo" label-width="80px">
          <!-- prop="subjectID"最新添加 -->
          <el-form-item label="所属学科">
            <el-select v-model="editDirectoryInfo.subjectName" placeholder="请选择" class="editSelect">
              <el-option
                v-for="item in subjectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称" prop="directoryName">
            <el-input v-model="editDirectoryInfo.directoryName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitEdit">确认</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import { list, add, changeState, remove, update } from '@/api/hmmm/tags.js'
import { simple as subjectSimple } from '@/api/hmmm/subjects.js'

export default {
  name: 'tags',
  data () {
    return {
      input: '',
      subjectListLabel: '',
      queryData: {
        query: '',
        pagesize: 10,
        page: 1,
        state: null
      },
      options: [
        {
          value: '0',
          label: '启用'
        },
        {
          value: '1',
          label: '禁用'
        }
      ],
      total: 0,
      tagForm: {},
      // 学科简单列表
      subjectList: [],
      tableData: [],
      tagsList: [],
      // 关于修改所有数据
      // 修改对话框校验规则
      editDirectoryInfo: {},
      editRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      // 学科id
      subjectID: '',
      tagName: '',
      // 关于添加所有的数据
      // 点击禁用显示高亮
      active: true,
      addTagsDialog: false,
      // 新增目录
      addTagsForm: {
        addTagsForm: ''
      },
      addDirectoryInfo: {
        directoryName: ''
      },
      // 校验规则
      addTagsFormRules: {
        tagName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      selectValue: ''
    }
  },

  created () {
    this.getTagsList()
  },

  methods: {
    // 获取标签详情列表
    async getTagsList () {
      const { data: res } = await list(this.queryData)
      this.tagsList = res.items
      this.total = res.counts
    },
    handleSizeChange (pagesize) {
      this.queryData.pagesize = pagesize
      this.getTagsList()
    },
    handleCurrentChange (page) {
      this.queryData.page = page
      this.getTagsList()
    },
    // 点击清空状态
    clearState () {
      this.queryData.state = null
      this.getTagsList()
    },
    // 改变状态
    // 如果不发送请求只是修改了本地数据,  this.getTagsList()又会重新刷新页面，你所修改都没有效果
    async showActive (row) {
      row.state = row.state ? 0 : 1
      const res = await changeState(row)
      if (res.status !== 200) return this.$message.error('操作失败')
      this.$message.success('操作成功')
    },
    // 点击显示高亮
    // showActive () {
    //   this.tagsList.state = !this.tagsList.state - 0
    //   // console.log(this.tagsList.state)
    // },
    // // d点击跳出对话框
    // showDialog () {
    //   this.addTagsDialog = true
    // },
    // // 获取学科简单列表
    // async getSubjectsList () {
    //   const { data: res } = await subjectSimple()
    //   this.subjectList = res
    //   // // console.log(this.subjectList)
    // },
    // 新增目录对话框
    async showDialog () {
      this.addTagsDialog = true
      const res = await subjectSimple()
      this.subjectList = res.data
      // console.log(this.subjectList)
    },
    // 确认添加目录
    async submitadd () {
      const res = await add(this.addDirectoryInfo)
      if (res.status !== 200) return this.$message.error('添加目录失败')
      this.$message.success('添加目录成功')
      this.editDialogVisible = false
      this.getTagsList()
    },
    // 删除
    deleteDirectory (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(row)
        if (res.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.getTagsList()
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    // 渲染对话框中的内容, 拿到一行的数据
    async editDirectory (row) {
      this.editDialogVisible = true
      // row赋值给model动态绑定的值
      this.editDirectoryInfo = row
      const res = await subjectSimple()
      this.subjectList = res.data
    },
    // 确认修改
    async submitEdit () {
      this.editDialogVisible = false
      const res = await update(this.editDirectoryInfo)
      if (res.status !== 200) return this.$message.error('操作失败')
      this.$message.success('操作成功')
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  /deep/.box-card {
    margin: 10px;
    .el-table__header-wrapper {
      margin-top: 20px;
    }
    .el-pagination {
      float: right;
     margin: 20px 0;
     margin-bottom: 20px;
    }
    .option {
      .active {
        color: rgb(140, 158, 255);
      }
    }
    .el-input--suffix .el-input__inner {
      padding-right: 30px !important;
    }
  }
}
</style>
